package com.ycMc.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    public final static SimpleDateFormat ymdNoDash = new SimpleDateFormat("yyyyMMdd");

    public final static SimpleDateFormat ymdDash = new SimpleDateFormat("yyyy-MM-dd");

    public final static SimpleDateFormat ymdHmsDash = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final static long hour24InMillis = 24 * 60 * 60 * 1000;
    private final static long hourInMillis=60 * 60 * 1000;
    private final static long minuteInMillis=60*1000;//1分钟
    private final static long millis=1000;  //一秒钟

    private final static long startMillis =  DateUtil.toDate("1998-01-01", ymdDash).getTime();

    public static String toString(Calendar cal, SimpleDateFormat dateFormat) {
        return dateFormat.format(cal.getTime());
    }

    public static String toString(Date date, SimpleDateFormat dateFormat) {
        return dateFormat.format(date);
    }

    public static int toDayInt(Calendar cal) {
        return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH);
    }

    public static Date toDate(String dateStr, SimpleDateFormat dateFormat) {
        try {
            return dateFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    public static long dayToMilliSeconds(int days) {
        return days * hour24InMillis;
    }
    public static long hourToMilliSeconds(int hours){
        return hours*hourInMillis;
    }
    public static long minuteToMilliSeconds(int minutes){return minutes*minuteInMillis;}
    public static long secondToMillisSeconds(int seconds){
        return seconds*millis;
    }

    public static int getDayOffset(Date start, Date end) {
        return (int) ((end.getTime() - startMillis) / hour24InMillis - (start.getTime() - startMillis) / hour24InMillis);
    }


    /**
     * 获得本月第一天0点时间
     * @return
     */
    public static Long getStartMillisCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTimeInMillis();
    }

    /**
     * 获得本月最后一天24点时间
     * @return
     */
    public static Long getEndMillisCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTimeInMillis();
    }

    /**
     * 根据当前时间获取当天的开始时间毫秒值
     *
     * @return
     */
    public static long getStartMillisEveryDay() {
        Calendar startDate = new GregorianCalendar();
        startDate.set(Calendar.HOUR_OF_DAY, 0);
        startDate.set(Calendar.MINUTE, 0);
        startDate.set(Calendar.SECOND, 0);
        return startDate.getTimeInMillis();
    }

    /**
     * 获取当天时间结束时间毫秒值
     * @return
     */
    public static long getEndMillisEveryDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        return cal.getTimeInMillis();
    }
    /**
     * 根据当前时间获取上月的开始时间毫秒值
     *
     * @return
     */
    public static long getStartMillisLastMonth() {
        Calendar date = new GregorianCalendar();
        date.add(Calendar.MONTH, -1);
        date.set(Calendar.DAY_OF_MONTH, 1);
        date.set(Calendar.AM_PM, Calendar.AM);
        date.set(Calendar.HOUR_OF_DAY, 0);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        return date.getTimeInMillis();
    }

    public static long getEndMillisLastMonth() {
        Calendar date = new GregorianCalendar();
        date.set(Calendar.DAY_OF_MONTH, 1);
        date.set(Calendar.HOUR_OF_DAY, 0);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        return date.getTimeInMillis();
    }

    public static Short getTheYear() {
        Calendar date = new GregorianCalendar();
        short year = (short) date.get(Calendar.YEAR);
        if ((date.get(Calendar.MONTH)) == 0) {
            year--;
        }
        return year;
    }

    public static byte getTheLastMonth() {
        Calendar date = new GregorianCalendar();

        return (byte) (date.get(Calendar.MONTH) - 1);
    }

    public static Long getMillisAYearAgo() {
        Calendar date = new GregorianCalendar();
        date.add(Calendar.YEAR, -1);
        return date.getTimeInMillis();
    }


    public static String dateFormat(long param)
    {
        Date date = new Date(param);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String result = sdf.format(date)+"";
        return result;
    }


    /**
    *Create by libo2
    *Time:2017/7/18 on 17:18
    *@Request :
    *@Response:
     * 获取年月日 秒 时间状态 ：例如 20170608120345
    **/
    public static String dateFormat2(long param)
    {
        Date date = new Date(param);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String result = sdf.format(date)+"";
        return result;
    }

    /**
     *Create by libo2
     *Time:2017/7/18 on 17:18
     *@Request :
     *@Response:
     * 获取年月日 秒 时间状态 ：例如 20170608120345
     **/
    public static String dateFormat3(long param)
    {
        Date date = new Date(param);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String result = sdf.format(date)+"";
        return result;
    }


    /**
    *Create by libo2
    *Time:2017/7/18 on 14:14
    *@Request :
    *@Response:
     * 根据月份数量获取数据
    **/
    public static String getMillisByMonthDay(int monthDay){

        long a =30L*24L*60L*60L*1000L*monthDay;

        long b = System.currentTimeMillis();

        return  Long.toString(b-a);
    }


    public static String dateToCorn(Long createTime){

        long nowTime = createTime;


        System.out.println(new Date(nowTime));

        String resultCorn = "";

        Date date = new java.sql.Date(nowTime);
        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(nowTime);
        int seconds = calendar.get(Calendar.SECOND);

        int minute=calendar.get(Calendar.MINUTE);

        int day =calendar.get(Calendar.DAY_OF_MONTH);

        int hours = calendar.get(Calendar.HOUR_OF_DAY);

        int month= calendar.get(Calendar.MONTH)+1;

        resultCorn = seconds+" "+minute+" "+hours+" "+day+" "+month+" ?";

        System.out.println(resultCorn);

        return  resultCorn;
    }
}
